package com.qg.wcsopcuaqj.taskPool;


import com.qg.wcsopcuaqj.business.entity.TWcsRgvAlarm;
import com.qg.wcsopcuaqj.business.service.ITWcsRgvAlarmService;
import com.qg.wcsopcuaqj.dispatch.constant.QGSConstant;
import com.qg.wcsopcuaqj.enums.rgv1.RGV1Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.rgv1.RGV1Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.enums.rgv2.RGV2Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.opcua.selfunion.Enum.PLCType;
import com.qg.wcsopcuaqj.opcua.selfunion.UAService;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;
import java.util.UUID;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/30 16:23
 * @Version 1.0
 *
 *
 * //对于报警 ，比对一下，如果报警内容不相同就触发。。
 */
public class AlarmDetectRunnable implements Runnable {
    private final static Logger logger = LoggerFactory.getLogger(AlarmDetectRunnable.class);
    private UAService uaService;
    private ITWcsRgvAlarmService alarmService;

    //上一次 alarmInfo  用于比对
    private String rgv1Last;
    private String rgv2Last;
    private static final String rgv1 = "rgv1";
    private static final String rgv2 = "rgv2";

    public AlarmDetectRunnable(UAService uaService, ITWcsRgvAlarmService alarmService){
        this.uaService = uaService;
        this.alarmService = alarmService;
        this.rgv1Last = "";
        this.rgv2Last = "";
    }

    public AlarmDetectRunnable(){

    }

    private Boolean isChange(String now,String rgvName){
        if(Objects.equals(rgvName, rgv1)){
            if(Objects.equals(rgv1Last, now)){
                return false;
            }else {
                rgv1Last = now;
                return true;
            }
        }else{
            if(Objects.equals(rgv2Last, now)){
                return false;
            }else {
                rgv2Last = now;
                return true;
            }
        }
    }

    @Override
    public void run() {
        Boolean initialVlue = false;
        Boolean aBoolean= null;


        while(true){
            try {
                Thread.sleep(3600000);
                Boolean rgv1Alarm = Boolean.valueOf(getValue(RGV1Plc2WcsVarEnum.Alarm));
                if(rgv1Alarm){
                    String rgv1AlarmInfo = Arrays.toString((Boolean[]) getValueObj(RGV1Plc2WcsVarEnum.AlarmInfo1).getValue());


                    if(isChange(rgv1AlarmInfo,rgv1)){
                        alarmCreate(rgv1AlarmInfo,rgv1);
                        logger.info("AlarmDetectRunnable.  监听到 rgv1 出现alarm，alarmInfo: " + rgv1AlarmInfo);
                    }
                }

                Thread.sleep(3600000);

                Boolean rgv2Alarm = Boolean.valueOf(getValue(RGV2Plc2WcsVarEnum.Alarm));
                if(rgv2Alarm){
                    String rgv2AlarmInfo = Arrays.toString((Boolean[]) getValueObj(RGV2Plc2WcsVarEnum.AlarmInfo1).getValue());
                    if(isChange(rgv2AlarmInfo,rgv2)){
                        alarmCreate(rgv2AlarmInfo,rgv2);
                        logger.info("AlarmDetectRunnable.  监听到 rgv2 出现alarm，alarmInfo: " + rgv2AlarmInfo);
                    }
                }
            } catch (Exception e) {

                logger.info("AlarmDetectRunnable 出现异常");
            }
        }
    }


    private void alarmCreate(String alarmInfo,String rgvName) throws Exception {
        //发出报警
        if(rgvName.equals(rgv1)){
            TWcsRgvAlarm alarm = new TWcsRgvAlarm();
            alarm.setAlarmInfo(alarmInfo);
            alarm.setIsTaskfinished(Boolean.parseBoolean(getValue(RGV1Plc2WcsVarEnum.TaskFinish))?1:0);
            alarm.setRgvName("rgv1");
            alarm.setIsSendfinished(Boolean.parseBoolean(getValue(RGV1Plc2WcsVarEnum.SendFinish))?1:0);
            alarm.setHasGoods(Boolean.parseBoolean(getValue(RGV1Plc2WcsVarEnum.Goods))?1:0);
            alarm.setIsPickfinished(Boolean.parseBoolean(getValue(RGV1Plc2WcsVarEnum.PickFinish))?1:0);
            alarm.setCreateTime(LocalDateTime.now());
            alarm.setUpdateTime(LocalDateTime.now());
            alarmService.getBaseMapper().insert(alarm);
        }else {
            TWcsRgvAlarm alarm = new TWcsRgvAlarm();
            alarm.setAlarmInfo(alarmInfo);
            alarm.setIsTaskfinished(Boolean.parseBoolean(getValue(RGV2Plc2WcsVarEnum.TaskFinish))?1:0);
            alarm.setRgvName("rgv2");
            alarm.setIsSendfinished(Boolean.parseBoolean(getValue(RGV2Plc2WcsVarEnum.SendFinish))?1:0);
            alarm.setHasGoods(Boolean.parseBoolean(getValue(RGV2Plc2WcsVarEnum.Goods))?1:0);
            alarm.setIsPickfinished(Boolean.parseBoolean(getValue(RGV2Plc2WcsVarEnum.PickFinish))?1:0);
            alarm.setCreateTime(LocalDateTime.now());
            alarm.setUpdateTime(LocalDateTime.now());
            alarmService.getBaseMapper().insert(alarm);
        }
    }

    private String getValue(RGV1Plc2WcsVarEnum var) throws Exception {

        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private Variant getValueObj(RGV1Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue();
    }
    private Variant getValueObj(RGV2Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue();
    }

    private String getValue(RGV2Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }
    private String getValue(RGV1Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }


    private Boolean setValue(RGV1Wcs2PlcVarEnum var,String dataType,Object newValue) throws Exception {
        return uaService.setValue(
                var.getNamespace(),
                var.getIdentity(),
                PLCType.valueOf(dataType).convertType(newValue),
                QGSConstant.plcName);
    }
    private Boolean setValue(RGV1Plc2WcsVarEnum var,String dataType,Object newValue) throws Exception {
        return uaService.setValue(
                var.getNamespace(),
                var.getIdentity(),
                PLCType.valueOf(dataType).convertType(newValue),
                QGSConstant.plcName
        );
    }


}
